/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.restaurant.entity.Area;
import org.springblade.modules.restaurant.entity.PerformanceRating;
import org.springblade.modules.restaurant.enums.PerformanceRatingTypeEnums;
import org.springblade.modules.restaurant.mapper.PerformanceRatingMapper;
import org.springblade.modules.restaurant.service.IAreaService;
import org.springblade.modules.restaurant.service.IPerformanceRatingService;
import org.springblade.modules.restaurant.service.ITaskRecordService;
import org.springblade.modules.restaurant.vo.PerformanceRatingVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 绩效评分 服务实现类
 *
 * @author Blade
 * @since 2025-02-06
 */
@Service
@AllArgsConstructor
public class PerformanceRatingServiceImpl extends BaseServiceImpl<PerformanceRatingMapper, PerformanceRating> implements IPerformanceRatingService {

	private IAreaService areaService;
	private ITaskRecordService taskRecordService;

	@Override
	public IPage<PerformanceRatingVO> selectPerformanceRatingPage(IPage<PerformanceRatingVO> page, PerformanceRatingVO performanceRating) {
		return page.setRecords(baseMapper.selectPerformanceRatingPage(page, performanceRating));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void genUserPerformanceRating(Date now) {
		// 获取当前时间月开始时间 以及结束时间
		Date startTime = DateUtil.beginOfMonth(now);
		Date endTime = DateUtil.endOfMonth(now);

		String month = DateUtil.format(now, "yyyy-MM");
		// 先删除当月数据
		baseMapper.removeMonthRatingByType(month,  PerformanceRatingTypeEnums.USER.getType());
		// 获取当月所有已审核任务记录 先统计用户绩效评分
		List<PerformanceRating> performanceRatings = baseMapper.selectUserPerformanceRating(startTime, endTime);
		if (performanceRatings.isEmpty()) {
			return;
		}
		List<PerformanceRating> list = new ArrayList<>();
		// 统计区域绩效评分
		performanceRatings.forEach(performanceRating -> {
			PerformanceRating item = new PerformanceRating();
			item.setType(PerformanceRatingTypeEnums.USER.getType());
			item.setMonth(month);
			item.setUserId(performanceRating.getUserId());
			item.setUserName(performanceRating.getUserName());
			item.setScore(performanceRating.getScore());
			item.setTenantId(performanceRating.getTenantId());
			list.add(item);
		});
		saveBatch(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void genAreaPerformanceRating(Date now) {

		// 获取当前时间月开始时间 以及结束时间
		DateTime startTime = DateUtil.beginOfMonth(now);
		DateTime endTime = DateUtil.endOfMonth(now);
		String month = DateUtil.format(now, "yyyy-MM");
		// 先删除当月数据
		baseMapper.removeMonthRatingByType(month,  PerformanceRatingTypeEnums.AREA.getType());

		List<Area> areas = areaService.lambdaQuery().eq(Area::getType, 1).list();

		List<PerformanceRating> list = new ArrayList<>();
		areas.forEach(area -> {
			// 根据areaId 获取子区域
			List<Long> areaIds = new ArrayList<>();
			areaIds.add(area.getId());
			List<Long> subAreaIds = areaService.lambdaQuery().eq(Area::getParentId, area.getId())
				.list().stream().map(Area::getId).toList();
			if (!subAreaIds.isEmpty()) {
				areaIds.addAll(subAreaIds);
			}
			BigDecimal score = taskRecordService.getAreaMonthScore(areaIds, startTime, endTime);
			PerformanceRating performanceRating = new PerformanceRating();
			performanceRating.setType(PerformanceRatingTypeEnums.AREA.getType());
			performanceRating.setMonth(month);
			performanceRating.setAreaId(area.getId());
			performanceRating.setAreaName(area.getAreaName());
			performanceRating.setScore(score);
			performanceRating.setTenantId(area.getTenantId());
			list.add(performanceRating);
		});

		saveBatch(list);
	}

}
